package com.dao;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.enums.Enums;
import com.jdbc.DBUtil;
import java.util.List;

public class AdminDao {
    //登录
    public AdminInfo login(String adminName,String password){
        String sql="select * from adminInfo  where state !='"+ Enums.AdminState.删除.getValue()+"' and  adminName=? and password=? " ;
        return DBUtil.getSingleObj(sql, AdminInfo.class, adminName,password);
    }

    /**
     * 检查用户名是否存在
     * @param adminName 传入用户名
     * @return 返回一个AdminInfo对象
     */
    public AdminInfo checkName(String adminName) {
        String sql="select * from adminInfo where adminName=?";
        return DBUtil.getSingleObj(sql, AdminInfo.class, adminName);
    }

    //添加用户
    public int addAdmin(AdminInfo admin) {
        String sql="insert into adminInfo (adminName,password,note,roleId,state) values(?,?,?,?,?)" ;
        Object[] params= {
                admin.getAdminName(),
                admin.getPassword(),
                admin.getNote(),
                admin.getRoleId(),
                admin.getState()
        };
        return DBUtil.update(sql, params);
    }
    //获取用户数量
    public int getAdminCount() {
        String sql = "select * from adminInfo where state!='"+Enums.AdminState.删除.getValue()+"'";

        return DBUtil.getList(sql, AdminInfo.class).size();
    }

    //获取用户列表
    public List<AdminInfo> getAllAdmin(PageInfo page){
        return DBUtil.getList("select * from AdminInfo where state!='"+ Enums.AdminState.删除.getValue() +"' limit ?,?", AdminInfo.class,page.getBeginRow(),page.getPageSize());
    }

    /**
     * 联合查询，
     * @param page 传入分页对象
     * @return 返回AdminInfo对象列表
     */
    public List<AdminInfo> roleAdminList(PageInfo page) {
        String sql = "select a.*,b.roleName as roleName from AdminInfo a left join RoleInfo b on a.roleId=b.id limit ?,?";
        return DBUtil.getList(sql,AdminInfo.class,page.getBeginRow(),page.getPageSize());
    }

    public AdminInfo getAdminInfoById(int id) {
        String sql = "select * from adminInfo where id=?";
        return DBUtil.getSingleObj(sql,AdminInfo.class,id);
    }

    public int updateAdminRoleById(int id, int roleId) {
        String sql = "update adminInfo set roleId=? where id=?";
        return DBUtil.update(sql,roleId,id);
    }

    /**
     * 根据id锁定用户
     * @param id 传入int型用户id
     * @return 成功返回1
     */
    public int lockAdminById(int id){
        String sql = "update adminInfo set state='"+ Enums.AdminState.锁定.getValue()+"' where id=?";
        return DBUtil.update(sql,id);
    }

    /**
     * 根据id解锁用户
     * @param id 传入int型用户id
     * @return 成功返回1
     */
    public int unlockAdminById(int id) {
        String sql = "update adminInfo set state='"+ Enums.AdminState.正常.getValue()+"' where id=?";
        return DBUtil.update(sql,id);
    }

    /**
     * 根据id删除用户
     * @param id 传入用户id
     * @return 成功返回1
     */
    public int deleteAdminByid(int id) {
        String sql = "update adminInfo set state='"+ Enums.AdminState.删除.getValue()+"' where id=?";
        return DBUtil.update(sql,id);
    }

    /**
     * 根据id修改用户的用户名和备注
     * @param id 用户id
     * @param adminName 用户名
     * @param note 用户备注
     * @return 成功返回1
     */
    public int updateAdminNameAndNoteById(int id, String adminName, String note) {
        String sql = "update adminInfo set adminName=?,note=? where id=?";
        return DBUtil.update(sql,adminName,note,id);
    }

    /**
     * 根据id修改用户密码
     * @param password 新密码
     * @param id 用户id
     * @return 成功返回1
     */
    public int updateAdminPasswordById(String password, int id) {
        String sql = "update adminInfo set password=? where id=?";
        return DBUtil.update(sql,password,id);
    }
}
